<?php

namespace Application\Site1Bundle;

use Symfony\Component\HttpKernel\Bundle\Bundle;
use Symfony\Components\DependencyInjection\ContainerInterface;
use Symfony\Components\DependencyInjection\ParameterBag\ParameterBagInterface;


class Site1Bundle extends Bundle
{
    public function boot()
    {
        // JUST MORE SYMFONY+DOCTRINE BUGS FIXES

        $driver = $this->container->get('doctrine.orm.metadata_driver.annotation');

        $bundleDirs = $this->container->getParameter('kernel.bundle_dirs');

        $aliasMap = array();
        foreach ($this->container->getParameter('kernel.bundles') as $className) {
            $tmp = dirname(str_replace('\\', '/', $className));
            $namespace = str_replace('/', '\\', dirname($tmp));
            $class = basename($tmp);



            if (strncmp("Symfony", $namespace, 7) != 0 && is_dir($dir = $bundleDirs[$namespace].'/'.$class.'/Entities')) {
                $driver->addPaths(array(\realpath($dir)));
                $aliasMap[$class] = $namespace.'\\'.$class.'\\Entities';
            }

            
        }

        foreach($aliasMap as $namespace)

        $this->container->getDoctrine_ORM_EntityManagerService()
                ->getConfiguration()->getMetadataDriverImpl()->addDriver($driver, $namespace);



        $this->container->get('fireice')->getModuleManager()->setModulesSearchPath(
            \realpath($this->container->getParameter('module.dir'))
        );

        // установка путей плагинов идёт в бандле ModuleBundle

        if(php_sapi_name() == 'cli')
        {
            return;
        }

        $fireice = $this->container->get('fireice');
        
        if($fireice->getFileCache()->contains($this->container->getParameter('fireice.route_generator.controller')))
        {
        	include_once $fireice->getFileCache()->getTargetDirectory() . DIRECTORY_SEPARATOR . 
        	   $this->container->getParameter('fireice.route_generator.controller');
        	
        	$dr = $fireice->getDynamicRouter();
        	$dr->load($fireice->getFileCache()->getTargetDirectory() . DIRECTORY_SEPARATOR . 
                    $this->container->getParameter('fireice.route_generator.collection_ser'));

            $this->container->get('router')->getRouteCollection()->addCollection(
                $fireice->getDynamicRouter()->getRouteCollection()
            );
            
        }
    }
}